package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.cache.SessionCacheSupport2;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.ConvertUtil;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.controller.dto.UserSessionVO;
import com.deyuanyun.pic.pipe.domain.PicPipePipeCross;
import com.deyuanyun.pic.pipe.domain.PicPipePipeCrossG;
import com.deyuanyun.pic.pipe.domain.PicPipePipeCrossGExample;
import com.deyuanyun.pic.pipe.domain.custom.CustomPicPipePipeCross;
import com.deyuanyun.pic.pipe.domain.exceldomain.CrossingOverInfoVO;
import com.deyuanyun.pic.pipe.mapper.CustomPicPipePipeCrossMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipePipeCrossGMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipePipeCrossMapper;
import com.deyuanyun.pic.pipe.service.PipeCrossService;
import com.deyuanyun.pic.support.biz.CodeUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2015/12/29.
 * 穿跨越
 */
@Service
public class PipeCrossServiceImpl implements PipeCrossService{
    static Logger logger=Logger.getLogger(PipeCrossServiceImpl.class);
    @Autowired
    private PicPipePipeCrossMapper crossMapper;
    @Autowired
    private PicPipePipeCrossGMapper crossGMapper;
    @Autowired
    private CustomPicPipePipeCrossMapper customPicPipePipeCrossMapper;

    @SuppressWarnings("unchecked")
	@Override
    public void insertPipeCross(List<CrossingOverInfoVO> crossingOverInfoVOList) throws Exception{
       // List<PicPipePipeCross> crosses = Lists.transform(crossingOverInfoVOList,f);
        List<PicPipePipeCross> pipeCrossList=ConvertUtil.transform(crossingOverInfoVOList,PicPipePipeCross.class);
        Asserts.assertTrue(ObjectUtil.isNotNullEmpty(pipeCrossList),"出错了，没有找到需要导入的记录");
        if (ObjectUtil.isNotNullEmpty(pipeCrossList)){
            Date crDaye=new Date();
            for (PicPipePipeCross pipeCross:pipeCrossList){
                Asserts.assertTrue(StringUtils.isNotBlank(pipeCross.getId()),"没有获取到主键id，导入失败");
                pipeCross.setIsNewest((short)0);
                pipeCross.setDeleted((short)0);
                pipeCross.setCreateDate(crDaye);
                UserSessionVO userSessionVO= SessionCacheSupport2.getSessionVO();
                pipeCross.setCreateUser(userSessionVO.getUsername());

                Map map=new HashMap();
                map.put("priUuid",pipeCross.getId());
                List<CrossingOverInfoVO> overInfoList= MyCollectionUtil.filterListByMap(crossingOverInfoVOList,map);
                Asserts.assertTrue(ObjectUtil.isNotNullEmpty(overInfoList),"导入出错了，没有找到对应的坐标记录");
                CrossingOverInfoVO overInfo=overInfoList.get(0);
                PicPipePipeCrossG crossG1=new PicPipePipeCrossG();
                crossG1.setId(CommonUtil.getUUID());
                crossG1.setSort(1);
                crossG1.setConvertType("BD");
                crossG1.setDeleted((short) 0);
                crossG1.setIsNewest((short)0);
                crossG1.setX(new BigDecimal(overInfo.getBegiinLineloopX()));
                crossG1.setY(new BigDecimal(overInfo.getBegiinLineloopY()));
                crossG1.setZ(new BigDecimal(overInfo.getBegiinLineloopZ()));
                crossG1.setRecorder(pipeCross.getRecorder());
                crossG1.setRecordtime(pipeCross.getRecordtime());
                crossG1.setPipePipecrossId(pipeCross.getId());

                PicPipePipeCrossG crossG2=new PicPipePipeCrossG();
                crossG2.setId(CommonUtil.getUUID());
                crossG2.setSort(2);
                crossG2.setConvertType("BD");
                crossG2.setDeleted((short) 0);
                crossG2.setIsNewest((short)0);
                crossG2.setX(new BigDecimal(overInfo.getEndLineloopX()));
                crossG2.setY(new BigDecimal(overInfo.getEndLineloopY()));
                crossG2.setZ(new BigDecimal(overInfo.getEndLineloopZ()));
                crossG2.setRecorder(pipeCross.getRecorder());
                crossG2.setRecordtime(pipeCross.getRecordtime());
                crossG2.setPipePipecrossId(pipeCross.getId());

                pipeCross.setDataStated("20");
                crossMapper.insert(pipeCross);
                crossG1.setDataStated("20");
                crossGMapper.insert(crossG1);
                crossG2.setDataStated("20");
                crossGMapper.insert(crossG2);
            }
        }

    }

    @Override
    public void delPipeCrossRecord(String id){
        int i= crossMapper.deleteByPrimaryKey(id);
        Asserts.assertTrue(i>0,"该记录已经不存在");
        PicPipePipeCrossGExample example=new PicPipePipeCrossGExample();
        example.createCriteria().andPipePipecrossIdEqualTo(id);
        crossGMapper.deleteByExample(example);
    }

@Override
public List<CustomPicPipePipeCross> queryPipeCrossList(Map map)throws Exception{
    List<CustomPicPipePipeCross> crossList=customPicPipePipeCrossMapper.queryPipeCrossOverList(map);
    CodeUtil.convertId2CodeName(crossList, new String[]{"crossingType", "crossingTarget"
            , "crossingMethod"
            , "pipeStableMeasures","coatingMaterial","isSleeve"
            , "material"});
        return crossList;
    }

    @Override
    public List<PicPipePipeCrossG> queryCrossGByCrossId(String crossId){
        if (StringUtils.isBlank(crossId)){return null;}
        PicPipePipeCrossGExample example=new PicPipePipeCrossGExample();
        example.createCriteria().andPipePipecrossIdEqualTo(crossId)
        .andIsNewestEqualTo((short)0).andDeletedEqualTo((short)0);
        return crossGMapper.selectByExample(example);
    }

}
